package org.gary.base.dao.impl;

import java.sql.SQLException;

import javax.annotation.Resource;

import org.gary.base.dao.ImageDao;
import org.gary.base.model.Image;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Component;

/**
 * 
 * @author lengreen
 *
 */
@Component("imageDao")
public class ImageDaoImpl implements ImageDao {
	private HibernateTemplate hibernateTemplate;
	private String hql;
	
	@Override
	public Image loadByName(String name) {
		setHql("from " + Image.class.getName() + " image where image.name = '" + name + "'");
		Image image = hibernateTemplate.execute(new HibernateCallback<Image>() {

			@Override
			public Image doInHibernate(Session session)
					throws HibernateException, SQLException {
				Query q = session.createQuery(getHql()).setCacheable(true);
				return (Image) q.uniqueResult();
			}
		});
		return image;
	}
	
	@Resource
	public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
		this.hibernateTemplate = hibernateTemplate;
	}

	public HibernateTemplate getHibernateTemplate() {
		return hibernateTemplate;
	}

	public void setHql(String hql) {
		this.hql = hql;
	}

	public String getHql() {
		return hql;
	}

}
